﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.ViewModel
{
    public class CreateViewModel
    {
        private string tableName;
        private string fileDir;
        public CreateViewModel(string tableName, string fileDir)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
        }

        public void Run()
        {
            var formatTableName = Helper.GetPascalCase(tableName).Substring(1);
            var iService = $"I{Helper.GetPascalCase(formatTableName)}Service";
            var privateIservice = Helper.GetCamelCase(iService.Substring(1));
            var fileName = $"{formatTableName}ViewModel";
            IList<string> contents = new List<string>();
            contents.Add($"using CommunityToolkit.Mvvm.ComponentModel;");
            contents.Add($"using CommunityToolkit.Mvvm.Input;");
            contents.Add($"using Hualong.TestWorldPro.Cache;");
            contents.Add($"using Hualong.TestWorldPro.Common;");
            contents.Add($"using Hualong.TestWorldPro.Common.Enums;");
            contents.Add($"using Hualong.TestWorldPro.Dto;");
            contents.Add($"using Hualong.TestWorldPro.Resources.Langs;");
            contents.Add($"using Hualong.TestWorldPro.Services.Navigation;");
            contents.Add($"using Hualong.TestWorldPro.Services.{formatTableName};");
            contents.Add($"using Hualong.TestWorldPro.Validations;");
            contents.Add($"using Microsoft.Extensions.Logging;");
            contents.Add($"using System.Collections.ObjectModel;");
            contents.Add("");
            contents.Add($"namespace Hualong.TestWorldPro.ViewModels.{formatTableName}");
            contents.Add("{");
            contents.Add($"    public partial class {formatTableName}ViewModel : BaseViewModel");
            contents.Add("    {");
            contents.Add($"        private readonly {iService} {privateIservice};");
            contents.Add("");
            contents.Add($"        public {fileName}(");
            contents.Add($"            {iService} {privateIservice},");
            contents.Add($"            INavigationService navigationService, ILogger<{formatTableName}ViewModel> logger) : base(navigationService, logger)");
            contents.Add("        {");
            contents.Add($"            this.{privateIservice} = {privateIservice};");
            contents.Add($"            AddValidations();");
            contents.Add("        }");
            contents.Add($"        private void AddValidations()");
            contents.Add("        {");
            contents.Add($"            try");
            contents.Add($"            {{");
            contents.Add($"                logger.LogInformation($\"{{nameof(AddValidations)}} [.]\");");
            contents.Add($"                //TODO:");
            contents.Add($"            }}");
            contents.Add($"            catch (Exception ex)");
            contents.Add($"            {{");
            contents.Add($"                logger.LogError(ex, ex.Message);");
            contents.Add($"            }}");
            contents.Add("        }");
            contents.Add("        [RelayCommand]");
            contents.Add($"        private async Task GoBackAsync()");
            contents.Add("        {");
            contents.Add($"            try");
            contents.Add($"            {{");
            contents.Add($"                logger.LogInformation($\"{{nameof(GoBackAsync)}} [.]\");");
            contents.Add($"                await Task.Delay(10);");
            contents.Add($"                //TODO:");
            contents.Add($"            }}");
            contents.Add($"            catch (Exception ex)");
            contents.Add($"            {{");
            contents.Add($"                logger.LogError(ex, ex.Message);");
            contents.Add($"            }}");
            contents.Add("        }");
            contents.Add("        [RelayCommand]");
            contents.Add($"        private async Task RefreshAsync()");
            contents.Add("        {");
            contents.Add($"            try");
            contents.Add($"            {{");
            contents.Add($"                logger.LogInformation($\"{{nameof(RefreshAsync)}} [.]\");");
            contents.Add($"                await Task.Delay(10);");
            contents.Add($"                //TODO:");
            contents.Add($"            }}");
            contents.Add($"            catch (Exception ex)");
            contents.Add($"            {{");
            contents.Add($"                logger.LogError(ex, ex.Message);");
            contents.Add($"            }}");
            contents.Add("        }");
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, fileName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{fileName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);

            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
